﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/Block.cs
//  *
//  ***************************************************************************

#endregion

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

using Haplous.Core;
using Haplous.Correctness;

using JetBrains.Annotations;

namespace Haplous
{
    /// <summary>
    ///     The basic class for all visual elements in a game that have size and position.
    /// </summary>
    [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
    [DebuggerDisplay("Block: Name={Name}; Position={_position.X),{_position.Y); Size={_size.Width),{_size.Height)")]
    public class Block : ElementBase, IBlock
    {
        /// <summary>
        ///     A name that uniquely identifies this text block.
        /// </summary>
// ReSharper disable once FieldCanBeMadeReadOnly.Local
        [NotNull]
        private readonly string _name;

        /// <summary>
        ///     The opacity of the block.
        /// </summary>
        [NotNull]
        private readonly ModeProperty<Opacity> _opacity;

        /// <summary>
        ///     The position of the element.
        /// </summary>
        [NotNull]
        private readonly ModeProperty<PositionInt32> _position;

        /// <summary>
        ///     The size of the element.
        /// </summary>
        [NotNull]
        private readonly ModeProperty<SizeInt32> _size;

        /// <summary>
        ///     The initial height of the block.
        /// </summary>
        private int _startingHeight;

        /// <summary>
        ///     The initial opacity of the block.
        /// </summary>
        private double _startingOpacity;

        /// <summary>
        ///     The initial width of the block.
        /// </summary>
        private int _startingWidth;

        /// <summary>
        ///     The X coordinate at the start of the game.
        /// </summary>
        private int _startingX;

        /// <summary>
        ///     The Y coordinate at the start of the game.
        /// </summary>
        private int _startingY;

        /// <summary>
        ///     Initializes a new instance of the <see cref="Block" /> class.
        /// </summary>
        /// <param name="game">
        ///     The game to which the class belongs.  The <paramref name="game" /> must not be
        ///     <see langword="null" />.
        /// </param>
        /// <param name="name">A name that uniquely identifies this block.</param>
        public Block([NotNull] Game game, [NotNull] string name) : base(game)
        {
            name.ValidateArgument("name", StringMustNotBe.NullOrEmptyOrWhiteSpace);
            _name = name;
            _position = new ModeProperty<PositionInt32>(game, "Position");
            _size = new ModeProperty<SizeInt32>(game, "Size");
            _opacity = new ModeProperty<Opacity>(game, "Opacity");
        }

        /// <summary>
        ///     Copies the property values for the element in one mode to another mode.
        /// </summary>
        /// <param name="source">The source mode of the property values.</param>
        /// <param name="destination">The destination mode of the property values.</param>
        /// <param name="excludedProperties">A list of the names of the properties to exclude from the copy.</param>
        public override void CopyModeProperties(Mode source, Mode destination, IEnumerable<string> excludedProperties)
        {
            base.CopyModeProperties(source, destination, excludedProperties);

            if((excludedProperties == null) || !excludedProperties.Contains("Opacity"))
                _opacity.CopyModeProperties(source, destination);
            if((excludedProperties == null) || !excludedProperties.Contains("Position"))
                _position.CopyModeProperties(source, destination);
            if((excludedProperties == null) || !excludedProperties.Contains("Size"))
                _size.CopyModeProperties(source, destination);
        }

        /// <summary>
        ///     The position of the element.
        /// </summary>
        public PositionInt32 Position
        {
            get { return _position.Value; }
        }

        /// <summary>
        ///     The size of the element.
        /// </summary>
        public SizeInt32 Size
        {
            get { return _size.Value; }
        }

        /// <summary>
        ///     Resets the position of this block to that it had at the start of the game.
        /// </summary>
        public virtual void Reset()
        {
            EnforceNotDisposed();
            _position.Value.Set(_startingX, _startingY);
            _size.Value.Set(_startingWidth, _startingHeight);
            _opacity.Value.Value = _startingOpacity;
        }

        /// <summary>
        ///     Snaps the current state of the block's properties for later restoration by a call to <see cref="IBlock.Reset" />.
        /// </summary>
        public virtual void Snap()
        {
            EnforceNotDisposed();
            _startingX = _position.Value.X;
            _startingY = _position.Value.Y;
            _startingWidth = _size.Value.Width;
            _startingHeight = _size.Value.Height;
            _startingOpacity = _opacity.Value.Value;
        }

        /// <summary>
        ///     The opacity of the block.
        /// </summary>
        public Opacity Opacity
        {
            get { return _opacity.Value; }
        }

        /// <summary>
        ///     Gets a unique name that can be used to identify this object.
        /// </summary>
        /// <value>The unique name of this item.</value>
        /// <remarks>The name must never be <see langword="null" />, empty or whitespace.</remarks>
        public override string Name
        {
            get { return _name; }
        }

        /// <summary>
        ///     Called when the game is about to start playing.
        /// </summary>
        protected override void OnGameStarting()
        {
            Snap();
        }
    }
}